package com.google.code.chaotic.updates;

import javax.jdo.PersistenceManager;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

import com.google.code.chaotic.model.AppProperty;
import com.google.code.chaotic.server.PmFactory;

public class FrontTierUpdatesApp extends Application {

	public static final String KEY_HOST_NAME = "backTierWeb.hostName";
	public static final String KEY_APP_PORT = "backTierWeb.appPort";
	public static final String KEY_CONTEXT_PATH = "backTierWeb.contextPath";

	/**
	 * Creates a root Restlet that will receive all incoming calls.
	 */
	@Override
	public Restlet createInboundRoot() {
		// Create a router Restlet that routes each call to a
		// new instance of HelloWorldResource.
		Router router = new Router(getContext());

		// Defines only one route
		router.attach("/start", FrontTierStarterResourceImpl.class);
		router.attach("/list", FrontTierUpdatesResourceImpl.class);

		return router;
	}

	public static String getBackTierUrl() {
		// return "http://localhost:8080";

		PersistenceManager pm = PmFactory.get().getPersistenceManager();
		AppProperty hostNameProp = pm.getObjectById(AppProperty.class,
				KEY_HOST_NAME);
		AppProperty appPortProp = pm.getObjectById(AppProperty.class,
				KEY_APP_PORT);
		AppProperty contextPathProp = pm.getObjectById(AppProperty.class,
				KEY_CONTEXT_PATH);

		return "http://" + hostNameProp.getValue() + ":"
				+ appPortProp.getValue() + contextPathProp.getValue();

	}
}